package com.turtle3d.exporter;

import javax.media.j3d.Appearance;
import javax.media.j3d.Material;
import javax.vecmath.Color3f;

/**
 * Color, opacity and illumination. 
 * 
 * @author Marek Paterczyk <marek.paterczyk@gmail.com>
 *
 */
public class ObjectMaterial
{
	public Color3f ambientColor, diffuseColor, specularColor;
	public int opacity=1, illumination=0;
	public String textureName;
	public ObjectMaterial(Color3f diffuse)
	{
		super();
		this.diffuseColor = diffuse;
		this.ambientColor = diffuse;
		this.specularColor = diffuse;
	}
	
	public ObjectMaterial(Material material){
		ambientColor = new Color3f();
		diffuseColor = new Color3f();
		specularColor = new Color3f();
		material.getAmbientColor(ambientColor);
		material.getSpecularColor(specularColor);
		material.getDiffuseColor(diffuseColor);		
	}
	
	public ObjectMaterial(Appearance appearance){
		this(appearance.getMaterial());
		if(appearance.getTexture()!=null)
			textureName = appearance.getTexture().getName();				
	}
	
	public boolean equals(Object o){
		ObjectMaterial m = (ObjectMaterial)o;
		
		return colorsEqual(ambientColor, m.ambientColor) && colorsEqual(diffuseColor, m.diffuseColor) && colorsEqual(specularColor, m.specularColor) && opacity==m.opacity && illumination==m.illumination;
				
	}
	
	public boolean colorsEqual(Color3f c1, Color3f c2){
		return c1.x==c2.x && c1.y==c2.y && c1.z==c2.z;
	}
	
	public int hashCode(){
		return (ambientColor.toString()+specularColor.toString()+diffuseColor.toString()).hashCode();
	}
	
}
